using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GroundSaw : MonoBehaviour
{
	public float damage = 5f;

	public float sleep = 0.5f;

	public float range = 0.5f;

	public AudioSource audioSource;

	public AudioClip[] hitAudio;

	public Weapon weapon;

	public Tool tool;

	private float activeAt;

	private Dictionary<GameObject, BodyPart> contactedBodyParts = new Dictionary<GameObject, BodyPart>();

	private void Start()
	{
		weapon.damage = damage;
		weapon.stoppingPower = 0f;
	}

	private void Update()
	{
		if (!(activeAt > Time.time) && tool.placed && contactedBodyParts.Count > 0)
		{
			BodyPart value = contactedBodyParts.First().Value;
			if (!value || !value.body || !value.body.zombie)
			{
				contactedBodyParts.Remove(contactedBodyParts.First().Key);
			}
			else if (!value.body.zombie.isDog || !value.body.zombie.isMoveable)
			{
				value.OnHit(weapon, Vector2.zero, base.transform.position + new Vector3(Random.Range(-0.2f, 0.2f), Random.Range(0.1f, 0.3f)));
				audioSource.PlayOneShot(hitAudio[Random.Range(0, hitAudio.Length)]);
				activeAt = Time.time + sleep;
			}
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.gameObject.tag == "Body" && collision.transform.position.y > base.transform.position.y && collision.TryGetComponent<BodyPart>(out var component) && (bool)component.body && !component.body.isDead && (!component.body.zombie || !(Mathf.Abs(component.body.zombie.transform.position.y - base.transform.position.y) > range)))
		{
			contactedBodyParts.Add(collision.gameObject, component);
		}
	}

	private void OnTriggerExit2D(Collider2D collision)
	{
		if (contactedBodyParts.ContainsKey(collision.gameObject))
		{
			contactedBodyParts.Remove(collision.gameObject);
		}
	}
}
